﻿<Serializable()> Public MustInherit Class SelfDrawShapeLayer
    Inherits Layer

    Public Sub New(ByVal ln As String)
        MyBase.new(ln, "")
    End Sub

    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub



    Protected MustOverride Sub DrawMySelf()






    Public Overrides Sub InitLayer(ByRef m As AxMapWinGIS.AxMap)


        mapwin = m




        '还好拥有共同的特征
        Dim sf As New MapWinGIS.Shapefile()
        sf.Open(fname)
        m_iLayer = mapwin.AddLayer(sf, True)

        mapwin.set_LayerKey(m_iLayer, lname)



        ' AddHandler mapwin.ExtentsChanged, AddressOf Me.OnExtentsChanged

        InitBaseProp()

        InitProp()

        Me.AllRedraw()

    End Sub


    '内部功能函数
    Protected Sub AllRedraw()
        Me.ClearAll()
        Me.DrawMySelf()
        Me.DrawText()
    End Sub

    Protected Sub ClearAll()

        Dim sfData As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        sfData.StartEditingShapes(True)

        sfData.EditClear()
        'While sfData.NumShapes > 0
        '    sfData.EditDeleteShape(0)
        'End While

        sfData.StopEditingShapes()

        mapwin.Redraw()

    End Sub


    '保存和加载
    Public Overrides Sub Save()

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        Dim bS As Boolean = sf.Save()
        If bS Then
            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If

    End Sub

    Public Overrides Sub SaveAs(ByVal strPath As String)


        Dim fn As String = strPath + lname + ".shp"

        Dim myPath(2) As String
        myPath(0) = strPath + lname + ".shp"
        myPath(1) = strPath + lname + ".dbf"
        myPath(2) = strPath + lname + ".shx"


        For i As Integer = 0 To myPath.Length - 1
            If System.IO.File.Exists(myPath(i)) Then
                Try
                    System.IO.File.SetAttributes(myPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(myPath(i))
                Catch ex As Exception

                End Try

            End If
        Next


        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        Dim bS As Boolean = sf.SaveAs(fn)


        If Not bS Then
            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If

        '这个时候文件句柄已经改变，所以需要取回原来的句柄
        Dim b As Boolean = sf.Open(fname)
        If Not b Then
            System.Windows.Forms.MessageBox.Show("回读文件失败，请不要删除或更改项目文件!")

        End If



    End Sub

    Public Overrides Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)

        fname = fPath + lname + ".shp"

        'Me.InitLayer(m)
        Me.initLoadedLayer(m)


    End Sub
    Public Sub initLoadedLayer(ByRef m As AxMapWinGIS.AxMap)
        mapwin = m




        '还好拥有共同的特征
        Dim sf As New MapWinGIS.Shapefile()
        sf.Open(fname)
        m_iLayer = mapwin.AddLayer(sf, True)

        mapwin.set_LayerKey(m_iLayer, lname)



        ' AddHandler mapwin.ExtentsChanged, AddressOf Me.OnExtentsChanged

        InitBaseProp()

        InitProp()

        Me.DrawText()




    End Sub


    Public Overrides Sub Dispose()

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        sf.Close()

    End Sub


End Class
